<!--
 Copyright (c) 2016,2017,2018,2019,2020,2021,2022 Klaus Landsdorf (http://node-red.plus/)
 All rights reserved.
 node-red-contrib-modbus - The BSD 3-Clause License

 @author <a href="mailto:klaus.landsdorf@bianco-royal.de">Klaus Landsdorf</a> (Bianco Royal)
-->
<script type="text/javascript">
  RED.nodes.registerType('modbus-server', {
    category: 'modbus',
    color: '#E9967A',
    defaults: {
      name: {value: ''},
      logEnabled: {value: false},
      hostname: {value: '0.0.0.0'},
      serverPort: {value: 10502, required: true, validate: RED.validators.number()},
      responseDelay: {value: 100, required: true, validate: RED.validators.number()},
      delayUnit: {
        value: 'ms', required: true, validate: function (v) {
          return 'ms' == v || 's' == v || 'm' == v || 'h' == v
        }
      },
      coilsBufferSize: {value: 10000, validate: RED.validators.number()},
      holdingBufferSize: {value: 10000, validate: RED.validators.number()},
      inputBufferSize: {value: 10000, validate: RED.validators.number()},
      discreteBufferSize: {value: 10000, validate: RED.validators.number()},
      showErrors: {value: false}
    },
    inputs: 1,
    outputs: 5,
    align: "right",
    icon: 'modbus.png',
    paletteLabel: 'Modbus-Server',
    label: function () {
      return this.name || 'Modbus Server'
    }
  })
</script>


<script type="text/x-red" data-template-name="modbus-server">
    <div class="form-row">
        <label for="node-input-name"><i class="icon-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-row">
        <label for="node-input-hostname"><i class="icon-tag"></i> <span data-i18n="modbus-contrib.label.hostname"></span></label>
        <input type="text" id="node-input-hostname" placeholder="0.0.0.0 (IPv4) or :: (IPv6)">
    </div>
    <div class="form-row">
        <label for="node-input-serverPort"><i class="icon-tag"></i> <span data-i18n="modbus-contrib.label.port"></span></label>
        <input type="text" id="node-input-serverPort" placeholder="2000 to 65536">
    </div>
    <!-- <div class="form-row">
        <label for="node-input-responseDelay"><i class="icon-tag"></i> <span data-i18n="modbus-contrib.label.responseDelay"></span></label>
        <input type="text" id="node-input-responseDelay" placeholder="100" style="max-width:120px">
        <select id="node-input-delayUnit" style="max-width:160px">
            <option value="ms">millisecond(s)</option>
            <option value="s">second(s)</option>
            <option value="m">minute(s)</option>
            <option value="h">hour(s)</option>
        </select>
    </div> -->
    <div class="form-row">
        <label for="node-input-coilsBufferSize"><i class="icon-tag"></i> <span data-i18n="modbus-contrib.label.coils"></span></label>
        <input type="text" id="node-input-coilsBufferSize" placeholder="1024">
    </div>
    <div class="form-row">
        <label for="node-input-holdingBufferSize"><i class="icon-tag"></i> <span data-i18n="modbus-contrib.label.holdings"></span></label>
        <input type="text" id="node-input-holdingBufferSize" placeholder="1024">
    </div>
    <div class="form-row">
        <label for="node-input-inputBufferSize"><i class="icon-tag"></i> <span data-i18n="modbus-contrib.label.inputs"></span></label>
        <input type="text" id="node-input-inputBufferSize" placeholder="1024">
    </div>
    <div class="form-row">
        <label for="node-input-discreteBufferSize"><i class="icon-tag"></i> <span data-i18n="modbus-contrib.label.discretes"></span></label>
        <input type="text" id="node-input-discreteBufferSize" placeholder="1024">
    </div>
    <hr>
    <div class="form-row">
        <label style="min-width:190px" for="node-input-showErrors"><i class="fa fa-th"></i> <span
        data-i18n="modbus-contrib.label.showErrors"></span></label>
        <input type="checkbox" id="node-input-showErrors" style="max-width:30px">
    </div>
    <div class="form-row">
        <label style="min-width:190px" for="node-input-logEnabled"><i class="fa fa-th"></i> <span data-i18n="modbus-contrib.label.logEnabled"></span></label>
        <input type="checkbox" id="node-input-logEnabled" style="max-width:30px">
    </div>

</script>


<script type="text/x-red" data-help-name="modbus-server">
    <p>Node to provide a Modbus TCP server based on node-modbus (jsmodbus) for testing.</p>

    <p>On injecting the server sends the Buffers to the separate outputs</p>
    <p>You can use the Modbus write nodes (FC) to write data to the server buffers.</p>
    <p>You can use the Modbus read nodes (FC) to read data from the server buffers.</p>

    <p>Output 1: holding Buffer, type, msg</p>
    <p>Output 2: coils Buffer, type, msg</p>
    <p>Output 3: input Buffer, type, msg</p>
    <p>Output 4: discrete Buffer, type, msg</p>
    <br>
    <p>Input:
        On injecting a special payload, you can write directly to any register.
        This should only be used if you want to simulate a Modbus client.
        <code>
            msg.payload = {
                'value': msg.payload,
                'register': 'holding',
                'address': 1 ,
                'disableMsgOutput' : 0
            };
            return msg;
        </code>
    </p>
    <p>The value could also be a list of UInt8 numbers and they will be written to the buffer.</p>
    <p>
        Valid registers are:
        <ul>
            <li>holding
            <li>coils
            <li>input
            <li>discrete
        </ul>
        Set disableMsgOutput if you want to disable the Server outputs when injecting.
    </p>

</script>
